#include "eeprom.h"
#include "flash.h"
#include "trans_task.h"

static eeprom_datas_t paras;

/**
 * @brief 系统低电量关机或重启时会调用此接口，需在内做好数据存储到EEPROM的准备
 *
 * @param value NULL
 * @param lenth 0
 */
static void pd_data_save_cb(void *value, size_t lenth)
{
    flash_init_clk(NULL);
    flash_sector_erase(eeprom_sector_num);
    Rtc_ReadDateTime(&paras.rtc_time);
    flash_write_bytes(eeprom_sector_num, (uint8_t *)&paras, sizeof(paras));
}

static void power_off_cb(void *value, size_t lenth)
{
    paras.power_on_flg = 0;
}
static void power_on_cb(void *value, size_t lenth)
{
    paras.power_on_flg = 1;
}

void eeprom_init()
{
    flash_init_clk(NULL);
    flash_read_bytes(eeprom_sector_num, (uint8_t *)&paras, sizeof(paras));
    if (paras.chek_head != eeprom_chek_head)
    {
        memset((void *)&paras, 0, sizeof(paras));
        paras.chek_head = eeprom_chek_head;
        for (size_t i = 0; i < 30; i++)
        {
            paras.alarm[i].id = i;
        }
        paras.rtc_time.u8Year = 0x23;
        paras.rtc_time.u8Month = 0x08;
        paras.rtc_time.u8Day = 0x25;
        paras.rtc_time.u8Hour = 0x12;
        paras.rtc_time.u8Minute = 0x34;
        paras.rtc_time.u8Second = 0x56;
    }
    key_value_register(NULL, "restart", pd_data_save_cb);
    key_value_register(NULL, "power_off", power_off_cb);
    key_value_register(NULL, "power_on", power_on_cb);
    paras.power_on_flg = 1;
}

stc_rtc_time_t *eeprom_get_rtc_time()
{
    return &paras.rtc_time;
}

uint16_t *eeprom_get_step_history(void)
{
    return paras.step;
}

void eeprom_change_alarm(alarm_paras_t *alarm)
{
    paras.alarm[alarm->id] = *alarm;
}

void eeprom_del_alarm(uint8_t id)
{
    paras.alarm[id].en_flg = 0;
    paras.alarm_cnt--;
}

void eeprom_new_alarm(alarm_paras_t *alarm)
{
    for (size_t i = 0; i < 30; i++)
    {
        if (paras.alarm->en_flg == 0)
        {
            paras.alarm[i] = *alarm;
            paras.alarm_cnt++;
            return;
        }
    }
    trans_packer_send_pack(esp_trans_get_handle(), "alarm_full", NULL, 0);
}

void eeprom_upload_alarm()
{
    if (paras.alarm_cnt == 0)
    {
        trans_packer_send_pack(esp_trans_get_handle(), "alarm_info", &paras.alarm_cnt, sizeof(paras.alarm_cnt));
        return;
    }

    uint8_t *buffer = malloc(paras.alarm_cnt * sizeof(alarm_paras_t) + 1);
    if (buffer == NULL)
    {
        trans_packer_send_pack(esp_trans_get_handle(), "alarm_info", &paras.alarm_cnt, sizeof(paras.alarm_cnt));
        return;
    }

    *buffer = paras.alarm_cnt;
    uint8_t *buffer_add = buffer + 1;

    for (size_t i = 0; i < 30; i++)
    {
        if (paras.alarm->en_flg)
        {
            memcpy(buffer_add, &paras.alarm[i], sizeof(alarm_paras_t));
            buffer_add += sizeof(alarm_paras_t);
        }
    }
    trans_packer_send_pack(esp_trans_get_handle(), "alarm_info", buffer, paras.alarm_cnt * sizeof(alarm_paras_t) + 1);

    free(buffer);
}

alarm_paras_t *eeprom_get_alarm_paras()
{
    return &paras.alarm[0];
}

uint8_t eeprom_get_sys_power_on_sta(void)
{
    return paras.power_on_flg;
}

uint8_t eeprom_get_step_cnt_flg(void)
{
    return paras.step_cnt_flg;
}

alarm_paras_t *eeprom_get_alarm_setting()
{
    return &paras.alarm_setting;
}

void eeprom_set_alarm_setting(alarm_paras_t *ala)
{
    paras.alarm_setting = *ala;
}

uint8_t eeprom_get_alarm_cnt(void)
{
    uint8_t cnt = 0;
    for (size_t i = 0; i < 30; i++)
    {
        if(paras.alarm[i].en_flg)
            cnt++;
    }
    return cnt;
}
uint8_t eeprom_get_alarm_ready(void)
{
    uint8_t cnt = 0;
    for (size_t i = 0; i < 30; i++)
    {
        if(paras.alarm[i].en_flg && paras.alarm[i].week_flg)
            cnt++;
    }
    return cnt;
}
